package org.dreamfly.core.dao.impl;

import org.dreamfly.core.bean.SzVideo;
import org.dreamfly.core.bean.SzVideoType;
import org.dreamfly.core.dao.VideoDAO;
import org.dreamfly.core.util.HTMLInputFilter;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;

import java.util.List;

import static org.hibernate.criterion.Restrictions.eq;
import static org.hibernate.criterion.Restrictions.like;
import static org.hibernate.criterion.Restrictions.or;

/**
 * 
 * @author Administrator
 *yao  做修改
 */
@SuppressWarnings("deprecation")
public class VideoDAOImpl extends BaseDAOImpl<SzVideo,Long> implements VideoDAO {

	@Override
	public List normalSearch(SzVideo szVideo,int firstRow,int maxRow) {
		Criteria criteria = this.getSession().createCriteria(SzVideo.class);
        criteria.add(Example.create(szVideo).enableLike(MatchMode.ANYWHERE));
        criteria.setFirstResult(firstRow);
    	criteria.setMaxResults(maxRow);
        List list = criteria.list();
        return null == list || list.isEmpty() ? null : list;
	}
	@Override
	public Long findCount(List<Long> videoTypes, String title, String serverName) {
		if(videoTypes==null||videoTypes.isEmpty())return 0L;
		StringBuffer bs =new StringBuffer();
		for(Long l:videoTypes)
		{
			bs.append(",");
			bs.append(l);
		}
		/*for(int i=0;i<videoTypes.size();i++)
			bs.append(","+videoTypes.get(i));*/
		StringBuffer sql= new StringBuffer();
		sql.append("select count(*) from "+SzVideo.class.getName()
		+" where typeId in("+bs.toString().substring(1)+")");
		
		/*String sql="select count(*) from "+SzVideo.class.getName()
		+" where videoTypeId in("+bs.toString().substring(1)+")";*/
		
		if(title!=null&&!title.isEmpty())
		//	sql+=" and title like '%"+title+"%'";
			sql.append(" and (title like '%" + title + "%'");
        sql.append("or note like");
            sql.append("'%");
			sql.append(title);
			sql.append("%')");
	    if(null != serverName){
            sql.append("and server = '");
			sql.append(serverName);
            sql.append("'");
        }
		List list=this.getSession().createQuery(sql.toString()).list();
		//return null==list?0L:Long.parseLong(list.get(0).toString());
		return (list==null)?0L:(Long)list.get(0);
	}

	@SuppressWarnings({ "unchecked"})
	@Override
	public List<SzVideo> findByType(List<Long> videoTypes, String title, int firstRow, int maxRow, String serverName) {
		if(videoTypes==null||videoTypes.isEmpty())
			return null;
    	Criteria criteria = this.getSession().createCriteria(SzVideo.class);
    	criteria.add(Expression.in("szVideoType.typeId", videoTypes));
        if(null != serverName){
        criteria.add(eq("server",serverName));
        }
    	/**
		 * 对tilte进行判断
		 */
		if (title == null || title.isEmpty())
			return null;
		title = title.replaceAll(" ", "");
		if (title.isEmpty())
			return null;
		
    	if(title!=null&&!title.isEmpty())
    		criteria.add(or(like("title", "%" + title + "%"), like("note", "%" + title + "%")));
    	/**
    	 * 分页
    	 */
    	criteria.addOrder(Order.asc("title"));
    	criteria.setFirstResult(firstRow);
    	criteria.setMaxResults(maxRow);
    	List list=criteria.list();
    	return ((null == list) || list.isEmpty()) ? null : list;
	}

    @Override
    public void saveInfo(SzVideo video) {
        HTMLInputFilter inputFilter = new HTMLInputFilter();
		Session session = this.getSession();
		Transaction tx = session.beginTransaction();
		
		SzVideo SzVideo = (SzVideo) session.load(SzVideo.class, video.getId());
		SzVideo.setTitle(inputFilter.filter(video.getTitle()));
        SzVideo.setServer(inputFilter.filter(video.getServer()));
        SzVideo.setNote(inputFilter.filter(video.getNote()));
		
        tx.commit();
		session.flush();
    }

    @Override
    public void changeType(Long videoId, SzVideoType type, String disrate) {
        Session session = this.getSession();
        Transaction tx = session.beginTransaction();

        SzVideo video = (SzVideo) session.load(SzVideo.class, videoId);
        video.setDisrate(disrate);
        video.setSzVideoType(type);

        tx.commit();
        session.flush();
    }

}
